package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.AnnouncementService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class AnnouncementManager {

    @Reference
    private AnnouncementService announcementService;

    /**
     * 展示所有公告列表
     * @param page
     * @param pageSize
     * @return
     */
    public PageBeanVo messagesAnnouncements(Integer page, Integer pageSize) {
        //1.查询到分页数据
        PageBeanVo pageBeanVo = announcementService.findAnnouncement(page,pageSize);

        //2.封装数据
        List<Announcement> items = (List<Announcement>) pageBeanVo.getItems();
        List<AnnouncementVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)){
            for (Announcement announcement : items) {
                //创建需要封装的vo对象
                AnnouncementVo announcementVo = new AnnouncementVo();
                BeanUtil.copyProperties(announcement,announcementVo);
                //转换日期
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                announcementVo.setCreateDate(simpleDateFormat.format(announcement.getCreated()));
                //放入集合
                voList.add(announcementVo);
            }
        }
        //3.封装
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }
}
